import DemoModule from './demomodule/Module';
import EventSituationModule from './eventsituation/Module';
import commonModule from './common/Module';
// 地图
export default class Map2D {
    private map: any = null;
    private opts: any = null;
    private components: any = null;
    /**
     * @param opts
     * @param opts.targetId
     * @param opts.mapConfig
     * @param opts.serviceConfig
     * @param opts.symbolConfig
     */
    constructor(opts: any) {
        //
        this.opts = opts;
    }


    public initMap(): void {
        const map = new G.Map(this.opts.mapConfig.map);
        map.init({
            targetId: this.opts.targetId,
        });
        this.map = map;
        this.map.setClusterScatter(false);
    }

    public init() {
        this.initMap();
        this.initBaseLayers();
        this.initControls();
    }

    public initBaseLayers() {
        const baseLayers = this.opts.mapConfig.baseLayers;
        const serviceConfig = this.opts.serviceConfig;
        let resthttp: any = null;
        if (serviceConfig && serviceConfig.egis) {
            const clientId = serviceConfig.egis.clientId;
            const clientSecret = serviceConfig.egis.clientSecret;
            resthttp = new g2.core.RestHttp({
                client_id: clientId,
                client_secret: clientSecret,
            });
        }
        const conf = baseLayers[0];
        /* const originLayers = conf.layers;
        if ( Array.isArray(originLayers)) {
            originLayers.forEach((item, index) => {
                if (item.tileType === 102 && !item.restHttp) {
                    item.restHttp = resthttp;
                }
            });
        } else {
            conf.restHttp = resthttp;
        } */
        const baseLayer = G.utils.LayerUtil.createBaseLayer(conf, resthttp);
        this.getMap().addLayer(baseLayer);
        console.log(baseLayer);
        // 初始化点线面图层组
        this.map.initialGroupLayers();
    }

    public initControls() {
        this.getMap().addScaleLineControl();
    }

    /**
     * 通用基础组件
     */
    public createComponents() {
        const popupManager: any = new G.common.PopupManager({
            map: this.getMap(),
            autoPanTimeout: 500,
            // 默认层级
            zIndex: 9,
            // 像素偏移量
            offset: [0, -30],
        });
        popupManager.load();
        // 要素定位
        const featureLocate: any = new G.common.FeatureLocate({
            map: this.getMap(),
            padding: new Array(4).fill(100),
            maxZoom: 16,
            duration: {
                move: 300,
                zoom: 500,
            },
        });
        featureLocate.load();
        // 要素高亮
        const featureHighlight: any = new G.common.FeatureHighlight({
            id: 'featureHighlight',
            name: 'featureHighlight',
            map: this.getMap(),
            blink: {
                interval: 200,
                duration: 3000,
            },
        });
        featureHighlight.load();
        // 图层管理器
        const layerManager: any = new G.common.LayerManager({
            map: this.getMap(),
        });
        layerManager.load();
        // 缓冲组件
        const bufferDraw: any = new G.interact.Buffer({
            map: this.getMap(),
            zIndex: 10,
            name: '缓冲区',
            close: {
                visible: true,
                style: {
                    type: 'PictureMarkerSymbol',
                    options: {
                        width: '41',
                        height: '41',
                        rotation: '0',
                        opacity: '1',
                        offsetX: 0,
                        offsetY: 0,
                        source: this.opts.symbolConfig.icons.bufferClose,
                    },
                },
            },
        });
        bufferDraw.load();
        // 简单渲染组件
        const simpleRenderMgr: any = new G.render.SimpleRenderMgr({
            map: this.getMap(),
        });
        simpleRenderMgr.load();
        // 点数据坐标检查的构建器
        const PointGeometryBuilder = G.utils.GeometryBuilder.extend({
            check: (geometry: any) => {
                // 统一进行坐标检查 ，过滤为零的坐标
                const check = G.utils.CoordUtil.verify([geometry.x, geometry.y], 4326);
                if (check) {
                    if (parseFloat(geometry.x) === 0 || parseFloat(geometry.y) === 0 ) {
                        return false;
                    }
                    return true;
                } else {
                    return false;
                }
            },
        });
        // 共享
        const components: any = {
            popupManager,
            featureLocate,
            featureHighlight,
            layerManager,
            simpleRenderMgr,
            bufferDraw,
            PointGeometryBuilder,
        };
        this.components = components;
        // test
        (window as any).GISComponents = components;
        return components;
    }

    /**
     * 业务功能组件
     * @param opts
     */
    public createBusinessComponents(opts: any) {
        const GISComponents = this.components;
        const symbolConfig = this.opts.symbolConfig;
        const moduleOpts = {
            map: this.getMap(),
            GISComponents,
            symbolConfig,
            mapConfig: this.opts.mapConfig,
        };
        // 示例
        const demo: any = new DemoModule(moduleOpts);
        // 事件态势
        const eventSituation: any = new EventSituationModule(moduleOpts);
        //
        const common: any = new commonModule(moduleOpts);
        const GISModules: any = {
            demo,
            eventSituation,
            common,
        };
        // test
        (window as any).GISModules = GISModules;
        return GISModules;
    }

    public getMap() {
        return this.map;
    }
}
